#ifndef _WX_HTMLCELL_H_
#define _WX_HTMLCELL_H_

#include "wx/defs.h"
#include "wx/html/htmltag.h"
#include "wx/html/htmldefs.h"
#include "wx/window.h"

class wxHtmlWindowInterface;
class wxHtmlLinkInfo;
class wxHtmlCell;
class wxHtmlContainerCell;

class wxHtmlSelection {
  public:
    wxHtmlSelection()
      : m_fromPos( wxDefaultPosition ), m_toPos( wxDefaultPosition ),
        m_fromPrivPos( wxDefaultPosition ), m_toPrivPos( wxDefaultPosition ),
        m_fromCell( NULL ), m_toCell( NULL ) {}

    void Set( const wxPoint& fromPos, const wxHtmlCell *fromCell,
              const wxPoint& toPos, const wxHtmlCell *toCell );
    void Set( const wxHtmlCell *fromCell, const wxHtmlCell *toCell );

    const wxHtmlCell *GetFromCell() const { return m_fromCell; }
    const wxHtmlCell *GetToCell() const { return m_toCell; }

    // these values are in absolute coordinates:
    const wxPoint& GetFromPos() const { return m_fromPos; }
    const wxPoint& GetToPos() const { return m_toPos; }

    // these are From/ToCell's private data
    const wxPoint& GetFromPrivPos() const { return m_fromPrivPos; }
    const wxPoint& GetToPrivPos() const { return m_toPrivPos; }
    void SetFromPrivPos( const wxPoint& pos ) { m_fromPrivPos = pos; }
    void SetToPrivPos( const wxPoint& pos ) { m_toPrivPos = pos; }
    void ClearPrivPos() { m_toPrivPos = m_fromPrivPos = wxDefaultPosition; }

    bool IsEmpty() const {
      return m_fromPos == wxDefaultPosition &&
             m_toPos == wxDefaultPosition;
    }

  private:
    wxPoint m_fromPos, m_toPos;
    wxPoint m_fromPrivPos, m_toPrivPos;
    const wxHtmlCell *m_fromCell, *m_toCell;
};



enum wxHtmlSelectionState {
  wxHTML_SEL_OUT,     // currently rendered cell is outside the selection
  wxHTML_SEL_IN,      // ... is inside selection
  wxHTML_SEL_CHANGING // ... is the cell on which selection state changes
};

// Selection state is passed to wxHtmlCell::Draw so that it can render itself
// differently e.g. when inside text selection or outside it.
class wxHtmlRenderingState {
  public:
    wxHtmlRenderingState() : m_selState( wxHTML_SEL_OUT ) {}

    void SetSelectionState( wxHtmlSelectionState s ) { m_selState = s; }
    wxHtmlSelectionState GetSelectionState() const { return m_selState; }

    void SetFgColour( const wxColour& c ) { m_fgColour = c; }
    const wxColour& GetFgColour() const { return m_fgColour; }
    void SetBgColour( const wxColour& c ) { m_bgColour = c; }
    const wxColour& GetBgColour() const { return m_bgColour; }

  private:
    wxHtmlSelectionState  m_selState;
    wxColour              m_fgColour, m_bgColour;
};

class wxHtmlRenderingStyle {
  public:
    virtual ~wxHtmlRenderingStyle() {}
    virtual wxColour GetSelectedTextColour( const wxColour& clr ) = 0;
    virtual wxColour GetSelectedTextBgColour( const wxColour& clr ) = 0;
};

// Standard style:
class wxDefaultHtmlRenderingStyle : public wxHtmlRenderingStyle {
  public:
    virtual wxColour GetSelectedTextColour( const wxColour& clr );
    virtual wxColour GetSelectedTextBgColour( const wxColour& clr );
};

class wxHtmlRenderingInfo {
  public:
    wxHtmlRenderingInfo() : m_selection( NULL ), m_style( NULL ) {}

    void SetSelection( wxHtmlSelection *s ) { m_selection = s; }
    wxHtmlSelection *GetSelection() const { return m_selection; }

    void SetStyle( wxHtmlRenderingStyle *style ) { m_style = style; }
    wxHtmlRenderingStyle& GetStyle() { return *m_style; }

    wxHtmlRenderingState& GetState() { return m_state; }

  protected:
    wxHtmlSelection      *m_selection;
    wxHtmlRenderingStyle *m_style;
    wxHtmlRenderingState m_state;
};

enum {
  wxHTML_FIND_EXACT             = 1,
  wxHTML_FIND_NEAREST_BEFORE    = 2,
  wxHTML_FIND_NEAREST_AFTER     = 4
};

enum wxHtmlScriptMode {
  wxHTML_SCRIPT_NORMAL,
  wxHTML_SCRIPT_SUB,
  wxHTML_SCRIPT_SUP
};

class wxHtmlCell : public wxObject {
  public:
    wxHtmlCell();
    virtual ~wxHtmlCell();

    void SetParent( wxHtmlContainerCell *p ) {m_Parent = p;}
    wxHtmlContainerCell *GetParent() const {return m_Parent;}

    int GetPosX() const {return m_PosX;}
    int GetPosY() const {return m_PosY;}
    int GetWidth() const {return m_Width;}

    // Returns the maximum possible length of the cell.
    // Call Layout at least once before using GetMaxTotalWidth()
    virtual int GetMaxTotalWidth() const { return m_Width; }

    int GetHeight() const {return m_Height;}
    int GetDescent() const {return m_Descent;}

    void SetScriptMode( wxHtmlScriptMode mode, long previousBase );
    wxHtmlScriptMode GetScriptMode() const { return m_ScriptMode; }
    long GetScriptBaseline() { return m_ScriptBaseline; }

    // Formatting cells are not visible on the screen, they only alter
    // renderer's state.
    bool IsFormattingCell() const { return m_Width == 0 && m_Height == 0; }

    const wxString& GetId() const { return m_id; }
    void SetId( const wxString& id ) { m_id = id; }

    // returns the link associated with this cell. The position is position
    // within the cell so it varies from 0 to m_Width, from 0 to m_Height
    virtual wxHtmlLinkInfo* GetLink( int ( x ) = 0,int ( y ) = 0 ) const{ return m_Link; }

    // Returns cursor to be used when mouse is over the cell:
    virtual wxCursor GetMouseCursor( wxHtmlWindowInterface *window ) const;

    // this was replaced by GetMouseCursor, don't use in new code!
    virtual wxCursor GetCursor() const;

    // return next cell among parent's cells
    wxHtmlCell *GetNext() const {return m_Next;}
    // returns first child cell (if there are any, i.e. if this is container):
    virtual wxHtmlCell* GetFirstChild() const { return NULL; }

    // members writing methods
    virtual void SetPos( int x, int y ) {m_PosX = x, m_PosY = y;}
    void SetLink( const wxHtmlLinkInfo& link );
    void SetNext( wxHtmlCell *cell ) {m_Next = cell;}
    virtual void Layout( int w );

    // renders the cell
    virtual void Draw( wxDC& ( dc ),int ( x ), int ( y ),int ( view_y1 ), int ( view_y2 ),wxHtmlRenderingInfo& ( info ) ) {}

    // proceed drawing actions in case the cell is not visible (scrolled out of
    // screen).  This is needed to change fonts, colors and so on.
    virtual void DrawInvisible( wxDC& ( dc ),
                                int ( x ), int ( y ),
                                wxHtmlRenderingInfo& ( info ) ) {}
    virtual const wxHtmlCell* Find( int condition, const void* param ) const;
    virtual bool ProcessMouseClick( wxHtmlWindowInterface *window,
                                    const wxPoint& pos,
                                    const wxMouseEvent& event );

    virtual void OnMouseClick( wxWindow *window,
                               int x, int y, const wxMouseEvent& event );
    virtual bool AdjustPagebreak( int *pagebreak,
                                  wxArrayInt& known_pagebreaks ) const;

    // Sets cell's behaviour on pagebreaks (see AdjustPagebreak). Default
    // is true - the cell can be split on two pages
    void SetCanLiveOnPagebreak( bool can ) { m_CanLiveOnPagebreak = can; }

    // Can the line be broken before this cell?
    virtual bool IsLinebreakAllowed() const
    { return !IsFormattingCell(); }

    // Returns true for simple == terminal cells, i.e. not composite ones.
    // This if for internal usage only and may disappear in future versions!
    virtual bool IsTerminalCell() const { return true; }
    virtual wxHtmlCell *FindCellByPos( wxCoord x, wxCoord y,
                                       unsigned flags = wxHTML_FIND_EXACT ) const;

    // Returns absolute position of the cell on HTML canvas.
    // If rootCell is provided, then it's considered to be the root of the
    // hierarchy and the returned value is relative to it.
    wxPoint GetAbsPos( wxHtmlCell *rootCell = NULL ) const;

    // Returns root cell of the hierarchy (i.e. grand-grand-...-parent that
    // doesn't have a parent itself)
    wxHtmlCell *GetRootCell() const;

    // Returns first (last) terminal cell inside this cell. It may return NULL,
    // but it is rare -- only if there are no terminals in the tree.
    virtual wxHtmlCell *GetFirstTerminal() const{ return wxConstCast( this, wxHtmlCell ); }
    virtual wxHtmlCell *GetLastTerminal() const{ return wxConstCast( this, wxHtmlCell ); }

    // Returns cell's depth, i.e. how far under the root cell it is
    // (if it is the root, depth is 0)
    unsigned GetDepth() const;
    bool IsBefore( wxHtmlCell *cell ) const;
    virtual wxString ConvertToText( wxHtmlSelection *( sel ) ) const{ return wxEmptyString; }

  protected:
    // pointer to the next cell
    wxHtmlCell *m_Next;
    // pointer to parent cell
    wxHtmlContainerCell *m_Parent;
    long m_Width, m_Height, m_Descent;
    // position where the fragment is drawn:
    long m_PosX, m_PosY;

    // superscript/subscript/normal:
    wxHtmlScriptMode m_ScriptMode;
    long m_ScriptBaseline;

    // destination address if this fragment is hypertext link, NULL otherwise
    wxHtmlLinkInfo *m_Link;
    // true if this cell can be placed on pagebreak, false otherwise
    bool m_CanLiveOnPagebreak;
    // unique identifier of the cell, generated from "id" property of tags
    wxString m_id;

    DECLARE_ABSTRACT_CLASS( wxHtmlCell )
    DECLARE_NO_COPY_CLASS( wxHtmlCell )
};

class wxHtmlWordCell : public wxHtmlCell {
  public:
    wxHtmlWordCell( const wxString& word, const wxDC& dc );
    void Draw( wxDC& dc, int x, int y, int view_y1, int view_y2,
               wxHtmlRenderingInfo& info );
    virtual wxCursor GetMouseCursor( wxHtmlWindowInterface *window ) const;
    wxString ConvertToText( wxHtmlSelection *sel ) const;
    bool IsLinebreakAllowed() const { return m_allowLinebreak; }

    void SetPreviousWord( wxHtmlWordCell *cell );

  protected:
    void SetSelectionPrivPos( const wxDC& dc, wxHtmlSelection *s ) const;
    void Split( const wxDC& dc,
                const wxPoint& selFrom, const wxPoint& selTo,
                unsigned& pos1, unsigned& pos2 ) const;

    wxString m_Word;
    bool     m_allowLinebreak;

    DECLARE_ABSTRACT_CLASS( wxHtmlWordCell )
    DECLARE_NO_COPY_CLASS( wxHtmlWordCell )
};

class wxHtmlContainerCell : public wxHtmlCell {
  public:
    wxHtmlContainerCell( wxHtmlContainerCell *parent );
    virtual ~wxHtmlContainerCell();

    virtual void Layout( int w );
    virtual void Draw( wxDC& dc, int x, int y, int view_y1, int view_y2,
                       wxHtmlRenderingInfo& info );
    virtual void DrawInvisible( wxDC& dc, int x, int y,
                                wxHtmlRenderingInfo& info );
    /*    virtual bool AdjustPagebreak(int *pagebreak, int *known_pagebreaks = NULL, int number_of_pages = 0) const;*/
    virtual bool AdjustPagebreak( int *pagebreak, wxArrayInt& known_pagebreaks ) const;

    // insert cell at the end of m_Cells list
    void InsertCell( wxHtmlCell *cell );

    // sets horizontal/vertical alignment
    void SetAlignHor( int al ) {m_AlignHor = al; m_LastLayout = -1;}
    int GetAlignHor() const {return m_AlignHor;}
    void SetAlignVer( int al ) {m_AlignVer = al; m_LastLayout = -1;}
    int GetAlignVer() const {return m_AlignVer;}

    // sets left-border indentation. units is one of wxHTML_UNITS_* constants
    // what is combination of wxHTML_INDENT_*
    void SetIndent( int i, int what, int units = wxHTML_UNITS_PIXELS );
    // returns the indentation. ind is one of wxHTML_INDENT_* constants
    int GetIndent( int ind ) const;
    // returns type of value returned by GetIndent(ind)
    int GetIndentUnits( int ind ) const;

    // sets alignment info based on given tag's params
    void SetAlign( const wxHtmlTag& tag );
    // sets floating width adjustment
    // (examples : 32 percent of parent container,
    // -15 pixels percent (this means 100 % - 15 pixels)
    void SetWidthFloat( int w, int units ) {m_WidthFloat = w; m_WidthFloatUnits = units; m_LastLayout = -1;}
    void SetWidthFloat( const wxHtmlTag& tag, double pixel_scale = 1.0 );
    // sets minimal height of this container.
    void SetMinHeight( int h, int align = wxHTML_ALIGN_TOP ) {m_MinHeight = h; m_MinHeightAlign = align; m_LastLayout = -1;}

    void SetBackgroundColour( const wxColour& clr ) {m_UseBkColour = true; m_BkColour = clr;}
    // returns background colour (of wxNullColour if none set), so that widgets can
    // adapt to it:
    wxColour GetBackgroundColour();
    void SetBorder( const wxColour& clr1, const wxColour& clr2 ) {m_UseBorder = true; m_BorderColour1 = clr1, m_BorderColour2 = clr2;}
    virtual wxHtmlLinkInfo* GetLink( int x = 0, int y = 0 ) const;
    virtual const wxHtmlCell* Find( int condition, const void* param ) const;
    virtual void OnMouseClick( wxWindow *window, int x, int y, const wxMouseEvent& event );
    virtual bool ProcessMouseClick( wxHtmlWindowInterface *window,const wxPoint& pos,const wxMouseEvent& event );

    virtual wxHtmlCell* GetFirstChild() const { return m_Cells; }
    wxDEPRECATED( wxHtmlCell* GetFirstCell() const );
    wxHtmlCell* GetLastChild() const { return m_LastCell; }

    // see comment in wxHtmlCell about this method
    virtual bool IsTerminalCell() const { return false; }

    virtual wxHtmlCell *FindCellByPos( wxCoord x, wxCoord y,unsigned flags = wxHTML_FIND_EXACT ) const;

    virtual wxHtmlCell *GetFirstTerminal() const;
    virtual wxHtmlCell *GetLastTerminal() const;
    virtual void RemoveExtraSpacing( bool top, bool bottom );
    virtual int GetMaxTotalWidth() const { return m_MaxTotalWidth; }
  protected:
    void UpdateRenderingStatePre( wxHtmlRenderingInfo& info,wxHtmlCell *cell ) const;
    void UpdateRenderingStatePost( wxHtmlRenderingInfo& info,wxHtmlCell *cell ) const;

  protected:
    int m_IndentLeft, m_IndentRight, m_IndentTop, m_IndentBottom;
    int m_MinHeight, m_MinHeightAlign;
    // minimal height.
    wxHtmlCell *m_Cells, *m_LastCell;
    // internal cells, m_Cells points to the first of them, m_LastCell to the last one.
    // (LastCell is needed only to speed-up InsertCell)
    int m_AlignHor, m_AlignVer;
    // alignment horizontal and vertical (left, center, right)
    int m_WidthFloat, m_WidthFloatUnits;
    // width float is used in adjustWidth
    bool m_UseBkColour;
    wxColour m_BkColour;
    // background color of this container
    bool m_UseBorder;
    wxColour m_BorderColour1, m_BorderColour2;
    // borders color of this container
    int m_LastLayout;
    // if != -1 then call to Layout may be no-op
    // if previous call to Layout has same argument
    int m_MaxTotalWidth;
    DECLARE_ABSTRACT_CLASS( wxHtmlContainerCell )
    DECLARE_NO_COPY_CLASS( wxHtmlContainerCell )
};

class wxHtmlColourCell : public wxHtmlCell {
  public:
    wxHtmlColourCell( const wxColour& clr, int flags = wxHTML_CLR_FOREGROUND ) : wxHtmlCell() {m_Colour = clr; m_Flags = flags;}
    virtual void Draw( wxDC& dc, int x, int y, int view_y1, int view_y2,wxHtmlRenderingInfo& info );
    virtual void DrawInvisible( wxDC& dc, int x, int y,wxHtmlRenderingInfo& info );

  protected:
    wxColour m_Colour;
    unsigned m_Flags;

    DECLARE_ABSTRACT_CLASS( wxHtmlColourCell )
    DECLARE_NO_COPY_CLASS( wxHtmlColourCell )
};

class wxHtmlFontCell : public wxHtmlCell {
  public:
    wxHtmlFontCell( wxFont *font ) : wxHtmlCell() { m_Font = ( *font ); }
    virtual void Draw( wxDC& dc, int x, int y, int view_y1, int view_y2,
                       wxHtmlRenderingInfo& info );
    virtual void DrawInvisible( wxDC& dc, int x, int y,
                                wxHtmlRenderingInfo& info );

  protected:
    wxFont m_Font;

    DECLARE_ABSTRACT_CLASS( wxHtmlFontCell )
    DECLARE_NO_COPY_CLASS( wxHtmlFontCell )
};

class wxHtmlWidgetCell : public wxHtmlCell {
  public:
    // !!! wnd must have correct parent!
    // if w != 0 then the m_Wnd has 'floating' width - it adjust
    // it's width according to parent container's width
    // (w is percent of parent's width)
    wxHtmlWidgetCell( wxWindow *wnd, int w = 0 );
    virtual ~wxHtmlWidgetCell() { m_Wnd->Destroy(); }
    virtual void Draw( wxDC& dc, int x, int y, int view_y1, int view_y2,
                       wxHtmlRenderingInfo& info );
    virtual void DrawInvisible( wxDC& dc, int x, int y,
                                wxHtmlRenderingInfo& info );
    virtual void Layout( int w );

  protected:
    wxWindow* m_Wnd;
    int m_WidthFloat;
    // width float is used in adjustWidth (it is in percents)

    DECLARE_ABSTRACT_CLASS( wxHtmlWidgetCell )
    DECLARE_NO_COPY_CLASS( wxHtmlWidgetCell )
};

class wxHtmlLinkInfo : public wxObject {
  public:
    wxHtmlLinkInfo() : wxObject() { m_Href = m_Target = wxEmptyString; m_Event = NULL, m_Cell = NULL; }
    wxHtmlLinkInfo( const wxString& href, const wxString& target = wxEmptyString ) : wxObject() { m_Href = href; m_Target = target; m_Event = NULL, m_Cell = NULL; }
    wxHtmlLinkInfo( const wxHtmlLinkInfo& l ) : wxObject() {
      m_Href = l.m_Href, m_Target = l.m_Target, m_Event = l.m_Event;
      m_Cell = l.m_Cell;
    }
    wxHtmlLinkInfo& operator=( const wxHtmlLinkInfo& l ) {
      m_Href = l.m_Href, m_Target = l.m_Target, m_Event = l.m_Event;
      m_Cell = l.m_Cell;
      return *this;
    }

    void SetEvent( const wxMouseEvent *e ) { m_Event = e; }
    void SetHtmlCell( const wxHtmlCell *e ) { m_Cell = e; }

    wxString GetHref() const { return m_Href; }
    wxString GetTarget() const { return m_Target; }
    const wxMouseEvent* GetEvent() const { return m_Event; }
    const wxHtmlCell* GetHtmlCell() const { return m_Cell; }

  private:
    wxString m_Href, m_Target;
    const wxMouseEvent *m_Event;
    const wxHtmlCell *m_Cell;
};

class wxHtmlTerminalCellsInterator {
  public:
    wxHtmlTerminalCellsInterator( const wxHtmlCell *from, const wxHtmlCell *to )
      : m_to( to ), m_pos( from ) {}

    operator bool() const { return m_pos != NULL; }
    const wxHtmlCell* operator++();
    const wxHtmlCell* operator->() const { return m_pos; }
    const wxHtmlCell* operator*() const { return m_pos; }

  private:
    const wxHtmlCell *m_to, *m_pos;
};

#endif
